
from flask_login import current_user
from flask_restful import Resource, marshal_with, reqparse
from controllers.errors.error import HTTPNotLoginError
from controllers.console import api
from core.roles.role_check import check_perms
from libs.login import login_required
from models.account import Account
from fields.robot.rob_func_fields import term_fields,term_pagenation_fields
from services.robot.intent_term_service import IntentRecTermService
class RobFuncsTermsApi(Resource):

    @login_required
    @check_perms(perms=['func_terms:list'])
    @marshal_with(term_pagenation_fields)
    def get(self):
        argparser = reqparse.RequestParser()
        argparser.add_argument("func_id",type=str,required=True,location="args")
        argparser.add_argument("term",type=str,required=False,default=None,location="args")
        argparser.add_argument("page",type=int,required=False,default=-1,location="args")
        argparser.add_argument("page_size",type=int,required=False,default=-1,location="args")
        args = argparser.parse_args()
        func_id = args.get("func_id","")
        term = args.get("term","")
        page = args.get("page",-1)
        page_size = args.get("page_size",-1)
        return IntentRecTermService.get_terms_by_cmd(func_id,term,page,page_size)

    @login_required
    @check_perms(perms=['func_terms:new'])
    @marshal_with(term_fields)
    def post(self):
        if not isinstance(current_user,Account):
            raise HTTPNotLoginError()
        argparser = reqparse.RequestParser()
        argparser.add_argument("func_id",type=str,required=True,location="json")
        argparser.add_argument("terms",type=str,action='append',required=True,location="json")
        args = argparser.parse_args()
        func_id = args.get("func_id","")
        terms = args.get("terms",[])
        return IntentRecTermService.create_terms(current_user,func_id,terms)
    
api.add_resource(RobFuncsTermsApi,"/rob/funcs/terms")